package cc.rengu.igas.cops.common.dao.impl;

import cc.rengu.igas.cops.common.dao.CopsPayOrderInfoHisMapper;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 支付订单历史表操作实现
 * @Author panky
 * @date 2020/5/16
 **/
public class CopsPayOrderInfoHisMapperImpl implements CopsPayOrderInfoHisMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    @Override
    public List<PayOrderInfo> selectPayOrderInfoHisByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderInfoHisByMchntNo");

        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntNo(mchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        return objects.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayHisOrderInfoBySysSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据系统流水号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrderHisInfoByInstIdTxnDateMchntOrderId(String instId, String txnDate, String mchntOrderId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "copsSelectPayOrderHisInfoByInstIdTxnDateMchntOrderId");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntOrderId(mchntOrderId);
        payOrderInfo.setTxnDate(txnDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构号、商户订单、交易日期查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, mchntOrderId);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }
}
